package com.example.gateway.config;

import com.example.gateway.filter.ValidateCodeGatewayFilter;
import com.example.gateway.handler.HystrixFallbackHandler;
import com.example.gateway.handler.ImageCodeHandler;
import com.example.gateway.handler.LanguageHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;

@Configuration
public class RoutesConfig {
    @Autowired
    private KeyResolver addressKeyResolver;
    @Autowired
    private HystrixFallbackHandler hystrixFallbackHandler;
    @Autowired
    private ImageCodeHandler imageCodeHandler;
    @Autowired
    private LanguageHandler languageHandler;
    @Autowired
    private ValidateCodeGatewayFilter validateCodeGatewayFilter;

    @Bean
    public RouterFunction routerFunction() {
        return RouterFunctions.route(
                RequestPredicates.path("/fallback")
                        .and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), hystrixFallbackHandler)
                .andRoute(RequestPredicates.GET("/code")
                        .and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), imageCodeHandler)
                .andRoute(RequestPredicates.GET("/changeLang")
                        .and(RequestPredicates.accept(MediaType.ALL)), languageHandler) ;

    }


    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("member", r -> r.path("/member/**")
                        .filters(f -> f.hystrix(
                                //服务熔断降级
                                h -> {
                                    h.setName("default");
                                    h.setFallbackUri("forward:/fallback");
                                }
                            )
//                            .requestRateLimiter(
//                                //限流配置
//                                l -> {
//                                    l.setKeyResolver(addressKeyResolver);
//                                    l.setRateLimiter(new RedisRateLimiter(10,20));
//                                }
//                            )
                        )
                        .uri("lb://member")
                       )
                .route("order",r ->r.path("/order/**")
                        .uri("lb://order"))
                .route("auth",r ->r.path("/auth/**")
                        .filters(f -> f.filter(validateCodeGatewayFilter))
                        .uri("lb://auth"))
                .build();
    }

}
